<?php
Yaf_loader::import("SDK/getui/IGt.Push.php");
$tConf = Yaf_Registry::get("config")->getui->default;
#define('APPKEY','PgpQ1Y0Pl26XZBFJuZOA79');
#define('APPID','9L962lenTk72TOlvz7Z023');
#define('MASTERSECRET','chHjMhADq99Mi8glEe3S81');

define('APPKEY',$tConf['appkey']);
define('APPID',$tConf['appid']);
define('MASTERSECRET',$tConf['mastersecret']);


define('HOST','http://sdk.open.api.igexin.com/apiex.htm');
define('taskId','OSS-0213_UA9Aq1pG1W97zj1zJ3q0e5');

$tCid = empty($_GET['cid'])?'':$_GET['cid'];
$tDevicetoken = empty($_GET['devicetoken'])?'':$_GET['devicetoken'];
$tContent = empty($_GET['content'])?'':$_GET['content'];
$tTitle = empty($_GET['title'])?'':$_GET['title'];
$tContent = mb_strcut($tContent, 0, 85, 'utf-8');
$tType = empty($_GET['type'])?'':$_GET['type'];
$tID = empty($_GET['id'])?'0':$_GET['id'];

define('CID',$tCid);
define('DEVICETOKEN',$tDevicetoken);
define('TITLE',$tTitle);
define('CONTENT',$tContent);
define('TYPE',$tType);#live 直播提醒  article 文章
define('ID',$tID);

class GetuiController extends Ctrl_Base{
	protected $_auth = 1;
	public function indexAction(){
        echo '<pre>';
        if(DEVICETOKEN){
            $this->ios();
		}else{
			$this->android();
		}
		exit;
	}
    private function IGtTransmissionTemplateDemo(){
        $template =  new IGtTransmissionTemplate();
        $template->set_appId(APPID);//应用appid
        $template->set_appkey(APPKEY);//应用appkey
        $template->set_transmissionType(2);//透传消息类型
        $template->set_transmissionContent("title=".TITLE.'&content='.CONTENT."&type=".TYPE."&id=".ID);//透传内容
		return $template;
    }
    /*private function IGtNotificationTemplateDemo(){
        $template =  new IGtNotificationTemplate();
        $template->set_appId(APPID);//应用appid
        $template->set_appkey(APPKEY);//应用appkey
        $template->set_transmissionType(2);//透传消息类型
        $template->set_transmissionContent("url=".url."&title=".title.'&nr='.nr."&type=".type."&id=".id);//透传内容
        return $template;
	}
     */
    private function ios(){
    
        $template =  new IGtTransmissionTemplate();
        $template->set_appId(APPID);//应用appid
        $template->set_appkey(APPKEY);//应用appkey
        $template->set_transmissionType(2);//透传消息类型
        #$template->set_transmissionContent("title=".title.'&nr='.nr."&type=".type."&id=".id);//透传内容
        $template->set_transmissionContent("title=".TITLE.'&content='.CONTENT."&type=".TYPE."&id=".ID);//透传内容
        #$template->set_pushInfo("actionLocKey","badge","message","sound","payload","locKey","locArgs","launchImage");

        $apn = new IGtAPNPayload();

        $alertmsg=new SimpleAlertMsg();
        $alertmsg->alertMsg="";
        $apn->alertMsg=$alertmsg;
        $apn->badge=2;
        $apn->sound="";
        $apn->add_customMsg("payload","title=".TITLE.'&content='.CONTENT."&type=".TYPE."&id=".ID);
#        $apn->contentAvailable=1;
        $apn->category="ACTIONABLE";
        $template->set_apnInfo($apn);

        $igt = new IGeTui(HOST,APPKEY,MASTERSECRET);
        $message = new IGtSingleMessage();
        $message->set_data($template);
        $ret = $igt->pushAPNMessageToSingle(APPID, DEVICETOKEN, $message);
        var_dump($ret);
         
    }

    private function android(){
         
        $template = $this->IGtTransmissionTemplateDemo();
        $message = new IGtSingleMessage();

        $message->set_isOffline(true);//是否离线
        $message->set_offlineExpireTime(3600*12*1000);//离线时间
        $message->set_data($template);//设置推送消息类型
        //接收方
        $target = new IGtTarget();
        $target->set_appId(APPID);
        $target->set_clientId(CID);


        $igt = new IGeTui(HOST, APPKEY, MASTERSECRET);
        try {
            $rep = $igt->pushMessageToSingle($message, $target);
            var_dump($rep);
            echo ("<br><br>");

        }catch(RequestException $e){
            $requstId =e.getRequestId();
            $rep = $igt->pushMessageToSingle($message, $target,$requstId);
            var_dump($rep);
            echo ("<br><br>");
        }
    }
}

